﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Tecnosop.WebUI.Models;
using System.Web.Security;

namespace Tecnosop.WebUI.Controllers
{
    public partial class AccountController : Controller
    {
        public virtual ActionResult LogOn()
        {
            return View();
        }

        [HttpPost]
        public virtual ActionResult LogOn(LogOnViewModel model, string returnUrl)
        {
            if (ModelState.IsValid)
            {
                if (FormsAuthentication.Authenticate(model.NombreUsuario, model.Password))
                {
                    FormsAuthentication.SetAuthCookie(model.NombreUsuario, false);
                    if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")
                        && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))
                    {
                        return Redirect(returnUrl);
                    }
                    else
                    {
                        return RedirectToAction(MVC.Home.ActionNames.Index, MVC.Home.Name);
                    }
                }
                else
                {
                    ModelState.AddModelError("", "El nombre de usuario o contraseña son incorrectos.");
                }
            }
            return View(model);
        }

        public virtual ActionResult LogOff()
        {
            FormsAuthentication.SignOut();

            return RedirectToAction(MVC.Home.ActionNames.Index, MVC.Home.Name);
        }
    }
}
